#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define DEBUG
inline int read()
{
	int x=0;char c=getchar();
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-48,c=getchar();
	return x;
}
inline void write(int x)
{
	if (x>=10) write(x/10);
	putchar(x%10+48);
}//快读快输

const int N = 1001;
int n, m;
int f[N];
int main()
{
	n=read(), m=read();
	for (int i = 0; i < m; i++)
	{
		int v=read(), w=read();
		for (int j = n; j >= v; j--) f[j] = max(f[j], f[j - v] + w);
	}
	write(f[n]);
	return 0;
}